

cd $directory


************************************************
*Summarize reversal margins for pivotal single states, two together, three together
************************************************


global LIST = "52201 69006 69106"

************************************************
*Loop over three datasets
************************************************

foreach file in $LIST{

	use "source_data`c(dirsep)'`file'.dta", clear

//count states 
local error=0
local counter=1
while `error' != 111 {
	qui cap d total_ev_`counter'
	local error=_rc
	local counter=`counter'+1
}
global STATECOUNT = `counter'-2 
di "$STATECOUNT"
local state_count = `counter'-2 


//make vars that summarize each election
forvalues i = 1(1)`state_count'{
	qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
	qui gen rwin_`i' = R_pvShare_`i' > .5
	qui gen rev_`i' = rwin_`i' * total_ev_`i'
	qui gen vmargin_`i' =  twoPartyVotes_`i'*abs(0.5- R_pvShare_`i')*2
	label var vmargin_`i' "The margin in vote counts in state `i'"
}
qui egen revn_total= rowtotal(rev_*)
qui gen rwin = revn_total/totalEV > 0.5


*************
* ONE STATE *
*************
qui gen cf_revn_total = .
qui gen cf_rwin = .
qui gen curr_min1 = .
qui gen flip1_state = .
qui gen use_i1 = .
qui gen use_i2 = .


//This loop identifies which states can flip election on their own (swing==1) and if so, 
//how many votes must be dropped to do it(vmargin==X)
forvalues i = 1(1)`state_count' {
	qui replace use_i1 = (rwin_`i' == rwin) & (vmargin_`i'<= curr_min1)
	*substract (add) state from (to) ECV count if R won (lost) the sim
	qui replace cf_revn_total = revn_total + (-1)^rwin_`i' * total_ev_`i'
	qui replace cf_rwin =  cf_revn_total/totalEV > 0.5 //did R win in the counterfactual flip of the state?
	qui replace use_i2 = (cf_rwin != rwin) & use_i1
	qui replace curr_min = vmargin_`i' if use_i2
	qui replace flip1_state = `i' if use_i2
}

qui gen min_vmargin1 = curr_min1
label var min_vmargin1 "minimum votes needed for 1-state flip"

drop use_* curr_min1

*/


*************
* TWO STATE *
*************

* initialize
qui gen cf_revn_total2 = .
qui gen curr_min2 = .
qui gen flip2_state1 = .
qui gen flip2_state2 = .
qui gen use_i = .
qui gen use_j1 = .
qui gen use_j2 = .

* outer loop
local state_count_m = `state_count' - 1
forvalues i = 1(1)`state_count_m' {
    qui replace use_i = (rwin_`i' == rwin) & (vmargin_`i'<= curr_min)
	*substract (add) state from (to) ECV count if R won (lost) the sim
	qui replace cf_revn_total = revn_total + (-1)^rwin_`i' * total_ev_`i' if use_i
	* inner loop
	local i_p = `i' + 1 
	forvalues j = `i_p'(1)`state_count' {
		qui replace use_j1 = (rwin_`j'== rwin_`i') & (vmargin_`j' + vmargin_`i' <= curr_min)
		* substract (add) state from (to) ECV count if R won (lost) the sim
		qui replace cf_revn_total2 = cf_revn_total + (-1)^rwin_`j' * total_ev_`j' if use_j1
		qui replace cf_rwin = cf_revn_total2/totalEV > 0.5 if use_j1 //did R win in the counterfactual flip of the states?
		
		qui replace use_j2 = (cf_rwin != rwin) & use_j1
		qui replace curr_min2 = vmargin_`j' + vmargin_`i' if use_j2
		qui replace flip2_state1 = `i' if use_j2
		qui replace flip2_state2 = `j' if use_j2
	}
}

qui gen min_vmargin2 = curr_min2
label var min_vmargin2 "minimum votes needed for 2-state flip"

drop use_* curr_min2

timer off 2
timer list 2


***************
* THREE STATE *
***************

* initialize
qui gen cf_revn_total3 = .
qui gen curr_min3 = .
qui gen flip3_state1 = .
qui gen flip3_state2 = .
qui gen flip3_state3 = .
qui gen use_i = .
qui gen use_j = .
qui gen use_k1 = .
qui gen use_k2 = .


local loop = 1
* outer loop
local state_count = 51
local state_count_m2 = `state_count' - 2
forvalues i = 1(1)`state_count_m2' {
    qui replace use_i = (rwin_`i' == rwin) & (vmargin_`i'<= curr_min3)
	*substract (add) state from (to) ECV count if R won (lost) the sim
	qui replace cf_revn_total = revn_total + (-1)^rwin_`i' * total_ev_`i' if use_i
	* middle loop
	local i_p = `i' + 1
	local state_count_m1 = `state_count' - 1
	forvalues j = `i_p'(1)`state_count_m1' {
		qui replace use_j = (rwin_`j'== rwin_`i') & (vmargin_`j' + vmargin_`i' <= curr_min3)
		*substract (add) state from (to) ECV count if R won (lost) the sim
		qui replace cf_revn_total2 = cf_revn_total + (-1)^rwin_`j' * total_ev_`j' if use_j
		* inner loop
		local j_p = `j' + 1 
		forvalues k = `j_p'(1)`state_count' {
		    qui replace use_k1 = (rwin_`k'== rwin_`j') & ///
								 (vmargin_`k' + vmargin_`j' + vmargin_`i' <= curr_min3)
			qui replace cf_revn_total3 = cf_revn_total2 + (-1)^rwin_`k' * total_ev_`k' if use_k1
			qui replace cf_rwin = cf_revn_total3/totalEV > 0.5 if use_k1
			qui replace use_k2 = (cf_rwin != rwin) & use_k1
			qui replace curr_min3 = vmargin_`k' + vmargin_`j' + vmargin_`i' if use_k2
			qui replace flip3_state1 = `i' if use_k2
			qui replace flip3_state2 = `j' if use_k2
			qui replace flip3_state3 = `k' if use_k2
			if mod(`loop',100) == 0 {
			    di "`loop'/19600 or so"
			}
			local loop = `loop'+1
		}
	}
}

qui gen min_vmargin3 = curr_min3
label var min_vmargin3 "minimum votes needed for 3-state flip"

drop use_* curr_min3

* save
	save "working_data`c(dirsep)'min_vote_reversal_upto3_`file'.dta", replace

* evaluate to see if the results make sense
sum min_vmargin* if !missing(min_vmargin1), d
sum min_vmargin2 min_vmargin3 if !missing(min_vmargin2), d

}

